package tempEngine.playerInput;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * an unsynchronized player input from keyboard to test the connection
 * process.
 * @author HINB's Project
 *
 */
public class TextPlayerForTest extends RealPlayer {
	/* *********************************************************
	 * InnerClasses
	 * Summary : 
	 * 		- AskingCoordThread
	 * ********************************************************* */
	/**
	 * class design to create an unsynchronized input for new coord
	 */
	private class AskingCoordThread extends Thread{
		
		/* *********************************************************
		 * Attributes
		 * ********************************************************* */
		private BufferedReader _in;
		private TextPlayerForTest _ply;
		private boolean _running;
		
		/* *********************************************************
		 * Constructor(s)
		 * ********************************************************* */
		public AskingCoordThread(TextPlayerForTest ply){
			_in = new BufferedReader(
					new InputStreamReader(System.in));
			_running = false;
			_ply = ply;
		}
		
		/* *********************************************************
		 * Instance Methods
		 * ********************************************************* */
		@Override
		public void run() {
			super.run();
			try{
				while (_running) {
					System.out.println("enter new Coord Please " +
							"(press Enter betwenn each value) :");
					boolean incorrect = true;
					int x = 0,y = 0;
					do{
						System.out.println("enter x-axis value");
						try{
							x = Integer.parseInt(_in.readLine());
							incorrect = false;
						}catch (NumberFormatException e) { } 
						
					}while(incorrect);
					do{
						System.out.println("enter y-axis value");
						try{
							y = Integer.parseInt(_in.readLine());
							incorrect = false;
						}catch (NumberFormatException e) { }
					}while(incorrect);
					//here we have the correct new coord :
					Point coord = new Point(x, y);
					_ply.setNewCoord(coord);
				}
			}catch (IOException e) {
				System.err.println("ouuuupppppssss not suppose to" +
						" happend : see TextPlayerForTest ...");
				// should not happend because we use the standart input
				e.printStackTrace();
			}
		}// end of Run Method
		
		public void setRunning(boolean running){
			_running = running;
		}
	}//end of AskingCoordThread
	/* *********************************************************
	 * End of InnerClasses
	 * ********************************************************* */

	/* *********************************************************
	 * Attributes
	 * ********************************************************* */
	private AskingCoordThread _askingThread;
	
	/* *********************************************************
	 * Constructor(s)
	 * ********************************************************* */

	public TextPlayerForTest(){
		_askingThread = new AskingCoordThread(this);
		_askingThread.setRunning(true);
		_askingThread.start();
	}
	
}
